package student.action;

import java.sql.Timestamp;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;

import edu.student.models.Announcements;
import edu.student.models.Interest;
import edu.student.models.News;
import edu.student.models.User;

public class EditProfile extends ActionSupport{
			
		private String username="";
		private String rollNo;
		private String email;
		private Date dob;
		private ArrayList<News> news;
		private ArrayList<Announcements> announcements;
		private String image="";
		private Timestamp lastLogin;
		private String userid="";
		private String commandButton;
		
		ArrayList<String> interestList = new ArrayList<String>();
		ArrayList<String> selInterestList = new ArrayList<String>();
		ArrayList<Interest> interests = new ArrayList<Interest>();

		public String getCommandButton() {
			return commandButton;
		}

		public void setCommandButton(String commandButton) {
			this.commandButton = commandButton;
		}

		public String getRollNo() {
			return rollNo;
		}

		public void setRollNo(String rollNo) {
			this.rollNo = rollNo;
		}

		public String getEmail() {
			return email;
		}

		public void setEmail(String email) {
			this.email = email;
		}

		public Date getDob() {
			return dob;
		}

		public void setDob(Date dob) {
			this.dob = dob;
		}

		public String getUserid() {
			return userid;
		}

		public void setUserid(String userid) {
			this.userid = userid;
		}

		public ArrayList<Announcements> getAnnouncements() {
				return announcements;
			}

			public void setAnnouncements(ArrayList<Announcements> announcements) {
				this.announcements = announcements;
			}

			public ArrayList<News> getNews() {
				return news;
			}

			public void setNews(ArrayList<News> news) {
				this.news = news;
			}
			public String getUsername() {
				return username;
			}

			public void setUsername(String username) {
				this.username = username;
			}
			

			public String getImage() {
				return image;
			}

			public void setImage(String image) {
				this.image = image;
			}
			
			/*public Timestamp getLastLogin() {
				return lastLogin;
			}

			public void setLastLogin(Timestamp lastLogin) {
				this.lastLogin = lastLogin;
			}*/
						
			
	   User user = new User();
	   public ArrayList<String> getInterestList() {
		return interestList;
	}

	public void setInterestList(ArrayList<String> interestList) {
		this.interestList = interestList;
	}

	public ArrayList<String> getSelInterestList() {
		return selInterestList;
	}

	public void setSelInterestList(ArrayList<String> selInterestList) {
		this.selInterestList = selInterestList;
	}

	public ArrayList<Interest> getInterests() {
		return interests;
	}

	public void setInterests(ArrayList<Interest> interests) {
		this.interests = interests;
	}

	User friend = new User();
		public String execute() throws Exception {
			
			HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
			commandButton = request.getParameter("commandButton");
			System.out.println("\n************************************ "+this.commandButton);
			if(this.commandButton!=null && this.commandButton.startsWith("Update")){
				System.out.println("calling update method");
			  update();			
			}
				System.out.println("in **************");
			Map<String, Object> session = ActionContext.getContext().getSession();
			User user = (User) session.get("user");
			if (user == null) {
				user = new User();
			}
			userid=user.getRollNor();
			username=user.getUsername();
			image=user.getImage();
			lastLogin= user.getLastLogin();
			
			System.out.println("Session User Roll Nor is"+ user.getRollNor());
			friend = User.findone("select * from user where roll_no = '" + userid+"'");
			//friend.setInterests(Interest.getInterests(" where u.roll_no='"+userid+"' "));
			
			//get all interests of a user
			interests = Interest.getInterests(" where u.roll_no='"+userid+"' ");
			System.out.println("\nInterests size = " + interests.size());
			for (int i = 0; i < interests.size(); i++){
			  Interest intr = interests.get(i);
			  System.out.println("Interest name = " + intr.getInterestName());
			  selInterestList.add(intr.getInterestName());
			}
					
		    //get all interests that a user has not included to his interest list
			interestList = UpdateProfile.findNotSelectedInterests(userid);
			
			System.out.println("\ninterestList size = " + interestList.size());
			System.out.println("\nselInterestList size = " + selInterestList.size());
			
			news = News.getNews(" where ui.roll_No='"+user.getRollNor()+"' ");
			announcements=Announcements.getAnnouncements(" where ui.roll_No='"+user.getRollNor()+"' ");
			return "success";
		}
		public void update(){
			System.out.println("in update fn");
			String query = "UPDATE USER SET USER_NAME='" +this.username + "', " + "EMAIL_ID='" + this.email + "', " +
			"DOB='" + this.dob + "' WHERE ROLL_NO='" + this.rollNo +"'";
			System.out.println(query);
			DB.update(query);
		    
			// return "update";
		}

		public User getUser() {
			return user;
		}

		public void setUser(User user) {
			this.user = user;
		}

		public User getFriend() {
			return friend;
		}

		public void setFriend(User friend) {
			this.friend = friend;
		}

}
